import path from 'path'
import requireDir from 'require-dir'
import {DbContext, classLoader, cacheProvider} from '@codefree/badger/service'

let context = {};
const config = require(path.normalize(__dirname + '/../config/config.dev.js'));
context.config = config;
context.cache = cacheProvider(config.cache);

const models = requireDir(path.join(config.baseDir, 'app/model'), {
    filter: function (fullPath) {
        return fullPath.match(/\.*.db.js$/)
    }
});

DbContext.initialize(config.db);
DbContext.getInstance().loadModels(models);
context.model = DbContext.models;
context.Sequelize = DbContext.Sequelize;
context.db = DbContext.getInstance().db;

const services = requireDir(path.join(config.baseDir, 'app/service'));
let serviceIns = new classLoader(context);
serviceIns.defineProperty('service', {user: {username: 'test', id: -1}}, {}, services);

export default serviceIns.service;
